// -----------------------------------------------------
//  validations
// -----------------------------------------------------
// 1 - required
// 2 - range
// 3 - pattern

var EditableDetails_Instances = [];

function EditableDetails(id)
{
    this.id = id;
        
    this.validatorID = "";
    this.tableItemsID = "";
    this.typeName = "";
    this.referenceDetail;

    this.removeButton = true;
    this.cloneButton = true;
    this.removeLabel = "";
    this.cloneLabel = "";
    this.trashIconPath = "Pampa.CRUD/trash.gif";
    this.duplicateIconPath = "Pampa.CRUD/duplicate.gif";
            	
    this.idParts = [];        
    this.details = [];
    this.validations = [];    
    
    this.maxIndex = 0;
    this.visibility = true;
    
    // Public Functions    
    this.show = EditableDetails_Show;
    this.hide = EditableDetails_Hide;    
    this.getRowID = EditableDetails_GetRowID;
    this.getControl = EditableDetails_GetControl;
    this.getValue = EditableDetails_GetValue;
    this.setValue = EditableDetails_SetValue;
    this.getDescription = EditableDetails_GetDescription;
    this.setDescription = EditableDetails_SetDescription;
    
    // Internal Functions
    this.addDetail = EditableDetails_AddDetail;
    this.removeDetail = EditableDetails_RemoveDetail;
    this.addValidation = EditableDetails_AddValidation;

    this.getCount = EditableDetails_GetCount;
    this.getValues = EditableDetails_GetValues;
    this.validate = EditableDetails_Validate;

    this.disableIdParts = EditableDetails_DisableIdParts;
    this.getRowIndex = EditableDetails_GetRowIndex;

    EditableDetails_Instances[id] = this;
} 


// --------------------------------------------------------------------------------------------------
// Public Functions
// --------------------------------------------------------------------------------------------------

function EditableDetails_Show()
{
    var cont = document.getElementById("panel_" + this.id);
    if(!cont)
        cont = document.getElementById(this.id);
        
    cont.style.display = '';
    this.visibility = true;    
}

function EditableDetails_Hide()
{
    var cont = document.getElementById("panel_" + this.id);
    if(!cont)
        cont = document.getElementById(this.id);
        
    cont.style.display = 'none';
    this.visibility = false;    
}

function EditableDetails_GetRowID(index)
{
    if ( (index < 0) || (index >= this.maxIndex) )
        return;
    else
        return this.details[index].id;
}

function EditableDetails_GetControl(idcontrol, rowID)
{
    var rowIndex = this.getRowIndex(rowID);
    if (rowIndex == -1)
        return;

    var control = this.details[rowIndex].getControl(idcontrol);
    return control;
}

function EditableDetails_GetValue(id, rowID)
{
    var rowIndex = this.getRowIndex(rowID);
    if (rowIndex == -1)
        return;
        
    var detail = this.details[rowIndex];
    var itemIndex = detail.getIndexControl(id);
    if(itemIndex==-1)
        return;
        
    var value = detail.getItemValue(itemIndex);
    return value;
}

function EditableDetails_SetValue(id, rowID, value)
{
    var rowIndex = this.getRowIndex(rowID);
    if (rowIndex == -1)
        return;
        
    var detail = this.details[rowIndex];
    var itemIndex = detail.getIndexControl(id);
    if(itemIndex==-1)
        return;

    detail.setItemValue(itemIndex, value);
}

function EditableDetails_GetDescription(id, rowID)
{
    var rowIndex = this.getRowIndex(rowID);
    if (rowIndex == -1)
        return;
        
    var detail = this.details[rowIndex];
    var itemIndex = detail.getIndexControl(id);
    if(itemIndex==-1)
        return;
    
    var description = detail.getItemDescription(itemIndex);
    return description;
}

function EditableDetails_SetDescription(id, rowID, description)
{
    var rowIndex = this.getRowIndex(rowID);
    if (rowIndex == -1)
        return;
        
    var detail = this.details[rowIndex];
    var itemIndex = detail.getIndexControl(id);
    if(itemIndex==-1)
        return;
            
    detail.setItemDescription(itemIndex, value);
}


// --------------------------------------------------------------------------------------------------
// Internal Functions
// --------------------------------------------------------------------------------------------------

function EditableDetails_AddDetail(rowID)
{
    var newid = this.id + "_ITEM" + this.maxIndex;
    var newDetail = this.referenceDetail.clone(newid);
    newDetail.parentID = this.id;
    newDetail.rowID = rowID;
    newDetail.trashIconPath = this.trashIconPath;
    newDetail.duplicateIconPath = this.duplicateIconPath;
    
    this.details.push(newDetail);
    this.maxIndex++;

    // Creo las validaciones para el nuevo item
    var validator = Validator_Instances[this.validatorID];
    for(var i=0; i<this.validations.length; i++) {
        var validation = this.validations[i];
        validator.addValidation(newDetail.id + "_" + validation.id, validation.type, validation.args, validation.msg);
    }
    
    return newDetail;
}

function EditableDetails_RemoveDetail(detailID)
{
    // Elimino las referencias
    var i=0;
    var findout = false;
    while( (i<this.details.length) && (!findout) ) {
        findout = (this.details[i].id == detailID);
        i++;
    }
    if(findout) {
        this.details[i-1].dispose();
        this.details.splice(i-1, 1);
    }

    // Elimino las validaciones del item eliminado
    var validator = Validator_Instances[this.validatorID];
    for(var i=0; i<this.validations.length; i++) {
        var validation = this.validations[i];
        validator.removeValidation(detailID + "_" + validation.id);
    }
    
    // Saco los objectos de la pagina
    var tableContainer = document.getElementById(this.tableItemsID);
    var tBody = tableContainer.children[0];
    var rowDetail = document.getElementById("row_" + detailID);
    tBody.removeChild(rowDetail);
}

function EditableDetails_AddValidation(id, type, args, errmsg)
{
    this.validations.push( {id: id, type: type, args: args, msg: errmsg} );
}

function EditableDetails_GetCount()
{
    return this.details.length;
}

function EditableDetails_GetValues(index)
{
    return this.details[index].getValues();
}

function EditableDetails_Validate()
{
    return Validate(this.validatorID);
}

// [IdParts]: apago los campos que son parte del id
function EditableDetails_DisableIdParts()
{
    for(var i=0; i<this.details.length; i++) {
        for(var j=0; j<this.idParts.length; j++) {
            this.details[i].disableItem(this.idParts[j]);
        }
    }
}


// -------------------------------------------------------------------------------------------
// Funciones Externas
// -------------------------------------------------------------------------------------------

function EditableDetails_AddItem(id)
{
    var mdetails = EditableDetails_Instances[id];
    var newDetail = mdetails.addDetail("");
    var tableContainer = document.getElementById(mdetails.tableItemsID);  
      
    newDetail.show(tableContainer, mdetails.removeButton, mdetails.removeLabel, mdetails.cloneButton, mdetails.cloneLabel);
    
    return newDetail; 
}

function EditableDetails_AddItemWithValues(id, values) 
{
    var mdetails = EditableDetails_Instances[id];
    var newDetail = mdetails.addDetail("");

    var tableContainer = document.getElementById(mdetails.tableItemsID);
    newDetail.show(tableContainer, mdetails.removeButton, mdetails.removeLabel, mdetails.cloneButton, mdetails.cloneLabel);

    // assigning values
    for (var propId in values) {
        var value = values[propId];
        var itemIndex = newDetail.getIndexControl(propId);
        if (itemIndex != -1) {
            newDetail.setItemValue(itemIndex, value);
        }
    }

    return newDetail; 
}

function EditableDetails_DuplicateItem(id, detailID)
{
    // Agrego el nuevo detalle
    var newDetail = EditableDetails_AddItem(id);
    
    // Copio los valores del detalle a duplicar
    var mdetails = EditableDetails_Instances[id];

    var i=0;
    var findout = false;
    while( (i<mdetails.details.length) && (!findout) ) {
        findout = (mdetails.details[i].id == detailID);
        i++;
    }
    if(findout) {
        var originalDetail = mdetails.details[i-1];
        newDetail.copyValues(originalDetail);
    }
}

function EditableDetails_RemoveItem(id, detailID)
{
    EditableDetails_Instances[id].removeDetail(detailID);
}

function EditableDetails_GetRowIndex(rowID)
{
    var findOut = false;
    var i = 0;
    
    while( (!findOut) && (i<this.details.length) ) {
        findOut = (rowID == this.details[i].id)
        if(!findOut)
            i++;    
    }
    
    if(findOut)
        return i;
    else
        return -1;
}